package com.petrochina.cloudconfig.util;

import com.petrochina.cloudconfig.domain.Pkg;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * package util
 */
public class PkgUtil {


    /**
     * 是否为包全名
     * @param pkgFullName
     * @return
     */
    public static boolean isPkgFullName(String pkgFullName){
         return  Pattern.matches("^([a-zA-z0-9]+\\.)+([a-zA-Z0-9]+-){1}([a-zA-Z0-9]+[.|-])+[j|w]ar$",pkgFullName);

    }

    /**
     * 解析pkg全名
     *
     * @param pkgFullName pkg全名 like com.petrochina.springbootuse-1.0.0-release.jar
     * @return Pkg 包对象
     */
    public static Pkg getPkg(String pkgFullName) {
        Pkg pkg;
        try {
            pkg = new Pkg();
            int firstLinkIndex = pkgFullName.indexOf("-");
            String groupAndArt = pkgFullName.substring(0, firstLinkIndex);
            int groupIndex = groupAndArt.lastIndexOf(".");
            pkg.setGroupId(groupAndArt.substring(0, groupIndex));
            pkg.setArtifactId(groupAndArt.substring(groupIndex + 1));
            String versionAndPkg = pkgFullName.substring(firstLinkIndex + 1);
            int versionIndex = versionAndPkg.lastIndexOf(".");
            pkg.setVersionId(versionAndPkg.substring(0, versionIndex));
            pkg.setPackaging(versionAndPkg.substring(versionIndex + 1));
            pkg.setPkgFullName(pkgFullName);
        } catch (Exception e) {
            e.printStackTrace();
            pkg = null;
        }
        return pkg;
    }


    public static String pkgToPath(Pkg pkg){
        StringBuilder sb = new StringBuilder();
        String groupId = pkg.getGroupId();
        String[] groupPaths = groupId.split(".");
        for (int i = 0; i < groupPaths.length; i++) {
            sb.append(groupPaths[i]);
            sb.append(File.separator);
        }
        sb.append(pkg.getArtifactId());
        sb.append(File.separator);
        sb.append(pkg.getVersionId());
        sb.append(File.separator);
        return sb.toString();
    }



    public static String getNameFromPkg(Pkg pkg) {
        StringBuilder sb = new StringBuilder();
        sb.append(pkg.getArtifactId());
        sb.append("-");
        sb.append(pkg.getVersionId());
        sb.append(".");
        sb.append(pkg.getPackaging());
        return sb.toString();
    }

    public static File getPkgFile(String pkgPath, String pkgFullName) {
        Pkg pkg = getPkg(pkgFullName);
        if(pkg != null){
            String filePath = pkgToPath(pkg);
            String fileName = PkgUtil.getNameFromPkg(pkg);
            return new File(pkgPath + File.separator + filePath + File.separator + fileName);
        }else{
            return null;
        }


    }


    public static String getPkgFullName(Pkg pkg) {
        StringBuilder sb = new StringBuilder();
        sb.append(pkg.getGroupId());
        sb.append(".");
        sb.append(pkg.getArtifactId());
        sb.append("-");
        sb.append(pkg.getVersionId());
        sb.append(".");
        sb.append(pkg.getPackaging());
        return sb.toString();

    }

}
